/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jheuristics.ga;

import org.jheuristics.util.Condition;

/**
 * TODO
 *
 * @author Marcell Manfrin, marcell@ourgrid.org, Jan 9, 2006
 */
public abstract class AbstractEvolver implements Evolver {
	private GAConfig config;
	private GAStatus status;

	/**
	 * TODO
	 *
	 * @param config
	 */
	public AbstractEvolver(GAConfig config) {
		setGAConfiguration(config);
	}

	/**
	 * TODO
	 *
	 * @param age
	 * @param current
	 * @param avaliable
	 * @param selected
	 * @return
	 */
	protected GAStatus createGAStatus(int age, Population current, Population avaliable, Population temp, Population selected) {
		if (0 > age) {
			// TODO: ERROR MSG
			throw new IllegalArgumentException();
		}
		current.sort();
		return new DefaultGAStatus(age, current, avaliable, temp, selected);
	}

	/**
	 * TODO
	 *
	 * @return
	 */
	protected Population createEmptyPopulation() {
		return new DefaultPopulation();
	}

	/**
	 * TODO 
	 */
	protected abstract void initialize();

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.Evolver#evolve()
	 */
	public void evolve() {
		if (null == status) {
		    initialize();
		}
	    config.getOperator().operate(status, config);
		config.getBulkFitnessEvaluator().evaluateFitness(status.getSelectedIndividualsPopulation(), status, config);
		status = createGAStatus(status.getEvolutionAge() + 1, status.getSelectedIndividualsPopulation(), createEmptyPopulation(), createEmptyPopulation(), createEmptyPopulation());
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.Evolver#evolve(int)
	 */
	public void evolve(int n) {
		for (int i = 0; i < n; i++)
			evolve();
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.Evolver#evolve(org.jheuristics.ga.Condition)
	 */
	public void evolve(Condition contition) {
		if (null == status) {
			initialize();
		}
		while (contition.check(new Object[] {status, config})) {
			evolve();
		}
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.Evolver#getEvolutionStatus()
	 */
	public GAStatus getEvolutionStatus() {
		return status;
	}

	/**
	 * TODO
	 *
	 * @param status
	 */
	protected void setEvolutionStatus(GAStatus status) {
		if (null == status) {
			// TODO: ERROR MSG
	        throw new NullPointerException();
		}
		this.status = status;
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.Evolver#getGAConfiguration()
	 */
	public GAConfig getGAConfiguration() {
		return config;
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.Evolver#setGAConfiguration(org.jheuristics.ga.GAConfig)
	 */
	public void setGAConfiguration(GAConfig config) {
	    if (null == config) {
			// TODO: ERROR MSG
	        throw new NullPointerException();
	    }
		this.config = config;
	}

}
